﻿using System;
using System.Collections.Generic;
using System.Text;
using GoldenAgeHelper.common;
using System.Text.RegularExpressions;
using GoldenAgeHelper.DAL;

namespace GoldenAgeHelper
{
    public class GAPosition
    {
        public static int cid = 0;
        public static string Building(int id, string action)
        {
           return Building(id, action, null);
        }

        public static string Building(int id,string action,string other)
        {
            string a = "";
            if (!string.IsNullOrEmpty(action))
            {
                a += "action=" + action + "&";
            }
            if (!string.IsNullOrEmpty(other))
            {
                a += other + "&";
            }
            string url = GA.ROOT_URL + "/building" + id + "?" + a + "ts=" + Util.GetTime();
            string text = GAClient.DownloadString(url);
            return text;
        }

        public static string Building(int id)
        {
            return Building(id, null);
        }

        public static string Envir(int id)
        {
            //http://t1.nexttime.cn/envir28?ts=1274929913718
            string text = GAClient.DownloadString(GA.ROOT_URL + "/envir" + id + "?ts=" + Util.GetTime());
            //收集场景信息
            if (false)
            {
                //>楚州郊外</a>
                string cityname = GARegex.GetString(text, "<h3><a href='#' u='/environ'>", "郊外</a>");
                int cid = City.GetCityID(cityname);

                if (EnvirConfig.Exists(cid, id))
                {
                    GA.AddMessage("此场景已经收集了信息。");
                    return text;
                }
                GA.AddMessage("开始收集[旅游/寻找生物]资料..");
                string pattern = "<p class='s15'>需要技能：\\[ (?<name>[\\s\\S]+?) \\] <span>(?<value>[0-9]+)</span></p>";
                //需要技能：[ 旅游 ] <span>101</span>
                string pattern1 = "需要技能：\\[(?<name>[\\s\\S]+?)\\] <span>(?<value>[0-9]+)</span>";
                MatchCollection coll = GARegex.Matches(text, pattern1);
                //需要技能：[ 旅游 ] <span>161</span></p></div>
                string pattern2 = "/sys/i/h/(?<id>\\d+?).gif' class='pic' />(?<name>[\\s\\S]+?)</a>";
                MatchCollection coll2 = GARegex.Matches(text, pattern2);
                for (int i = 0; i < coll.Count; i++)
                {
                    string ename = coll[i].Groups["name"].Value.Trim();
                    string rank = coll[i].Groups["value"].Value.Trim();
                    EnvirConfig.AddEnvirConfig(cid, id, ename, Convert.ToInt32(rank), coll.Count == coll2.Count ? coll2[i].Groups["name"].Value : "");
                }
                GA.AddMessage("共收集到旅游场所[" + coll.Count + "]个。");
                //需要技能：[ 采集 ] 1</p>
                coll = GARegex.Matches(text, "需要技能：\\[ (?<name>[\\s\\S]+?) \\] (?<value>[\\d+])</p>");
                for (int i = 0; i < coll.Count; i++)
                {
                    EnvirConfig.AddEnvirConfig(cid, id, coll[i].Groups["name"].Value, Convert.ToInt32(coll[i].Groups["value"].Value), coll.Count == coll2.Count ? coll2[i].Groups["name"].Value : "");
                }
                GA.AddMessage("共收集到采集[" + coll.Count + "]个。");
            }
            return text;
        }

        public static string Envir(Envir envir)
        {
            return Envir((int)envir);
        }

        public static string environ()
        {
            string text = GAClient.DownloadString(GA.ROOT_URL + "/environ?ts=" + Util.GetTime());
            return text;
        }

    }
}
